//require CN1Calendar
package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.calendar.DeviceCalendar;
import com.codename1.calendar.EventInfo;
import com.codename1.io.Preferences;
import com.codename1.l10n.L10NManager;
import com.codename1.ui.Button;
import com.codename1.ui.Label;
import java.util.Collection;
import java.util.Date;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class DeviceCalendarSample {

    private Resources theme;
    private Form current;
    
    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Calendar", BoxLayout.y());
        DeviceCalendar dc = DeviceCalendar.getInstance();
        if(dc == null) {
            hi.add("Device Calendar is null");
            hi.show();
            return;
        }
        if(!dc.hasPermissions()) {
            hi.add("No Calendar Access Permission");
            hi.show();
            return;
        }
        Collection<String> cals = dc.getCalendars();
        if(cals == null || cals.isEmpty()) {
            hi.add("No Calendars found on the device");
            hi.show();
            return;
        }
        
        int week = 7 * 24 * 60 * 60000;
        Date lastWeek = new Date(System.currentTimeMillis() - week);
        Date nextWeek = new Date(System.currentTimeMillis() - week);
        
        for(String s : cals) {
            Button b = new Button(s);
            b.addActionListener(e -> {
                String calId = dc.openCalendar(s, false);
                Collection<EventInfo> events = dc.getEvents(calId, lastWeek, nextWeek);
                Form f = new Form("Events", BoxLayout.y());
                for(EventInfo ei : events) {
                    f.add(new Label(
                        L10NManager.getInstance().formatDateTimeShort(ei.getStartTime()) + 
                            ei.getTitle()));
                }
                f.getToolbar().setBackCommand("", ee -> hi.showBack());
                f.show();
            });
            hi.add(b);
        }
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
